1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class TestForm (Form) :
t1 = fields.CharField(
widget=widgets.Textarea
)
t2 = fields.CharField(
widget=widgets.CheckboxInput
)
t3 = fields.MultipleChoiceField(
choices=[(1 , '篮球' ), (2 , '足球' )],
widget=widgets.CheckboxSelectMultiple
)
t4 = fields.ChoiceField(
choices=[(1 , '篮球' ), (2 , '足球' )],
widget=widgets.RadioSelect
)
t5 = fields.FileField(
widget=widgets.FileInput
)
def test (request) :
obj = TestForm(initial={'t3' : [2 , ]})
return render(request, 'test.html' , {'obj' : obj})
前端1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang ="en" >
<head >
<meta charset ="UTF-8" >
<title > Title</title >
</head >
<body >
{{ obj.t1 }}
{{ obj.t2 }}
{{ obj.t3 }}
{{ obj.t4 }}
{{ obj.t5 }}
</body >
</html >
关键点是返回值
进行正则验证
循环所有的字段,加入self.fields中并执行相应的函数clean_xxx,xxx是字段的名字
自己定义的高级验证一定要有返回值,默认是自己
源码的寻找循序:is_valid -> errors -> full_clean -> self._clean_fields()
源码中的full_clean1
2
3
self._clean_fields()
self._clean_form()
self._post_clean()
1 自定义函数进行验证
自定义的函数是对每个字段进行验证,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.core.exceptions import ValidationError
class TestForm (Form) :
user = fields.CharField()
pwd = fields.CharField()
def clean_user (self) :
v = self.cleaned_data['user' ]
if models.Students.objects.filter(name=v).count():
raise ValidationError('用户名已经存在' , code='invalid' )
else :
pass
return self.cleaned_data['user' ]
def clean_pwd (self) :
return self.cleaned_data['pwd' ]
2 整体验证
使用clean对整体进行验证,内部已经提供了异常处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class TestForm (Form) :
user = fields.CharField()
pwd = fields.CharField()
def clean_user (self) :
v = self.cleaned_data['user' ]
if models.Students.objects.filter(name=v).count():
raise ValidationError('用户名已经存在' , code='invalid' )
else :
pass
return self.cleaned_data['user' ]
def clean_pwd (self) :
return self.cleaned_data['pwd' ]
def clean (self) :
user = self.cleaned_data.get('user' )
email = self.cleaned_data.get('email' )
if models.Students.objects.filter(user=user, email=email).count():
raise ValidationError('用户名和邮箱联合存在' )
return self.cleaned_data
3 在默认正则中添加正则
需要导入一个模块
关键在validators=[],在内部添加正则,如第一个没有通过验证,后面的也就不用进行验证了
1
2
3
4
5
6
7
8
from django.core.validators import RegexValidator
class TestForm (Form) :
user = fields.CharField(
validators=[RegexValidator(r'^[0-9]+$' , '请输入数字' ), RegexValidator(r'^159[0-9]+$' , '数字必须以159开头' )],
)
pwd = fields.CharField()
总结:
字段 = 默认正则表达式
额外的正则
from django.forms import Form
from django.forms import widgets
from django.forms import fields
from django.core.validators import RegexValidator
class MyForm(Form):
user = fields.CharField(
validators=[RegexValidator(r'^[0-9]+$', '请输入数字'), RegexValidator(r'^159[0-9]+$', '数字必须以159开头')],
)
clean_字段,必须返回值
clean() 有返回值:cleaned_data = 返回值 无返回值:cleaned_data = 原来的值
参考:http://www.cnblogs.com/wupeiqi/articles/6144178.html